/*
 * Name        : BaseRequest.java
 * Classes     : BaseRequest
 * Version     : 1.0
 * Date        : 2013-08-29
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http;

import com.xgkj.diyiketang.http.engine.RequestEventListener;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;


/**
 * Class Name  : BaseRequest
 * Author      : Tu Yuanlin
 * Date        : 13-9-3
 * Description :
 */
public abstract class BaseRequest {

    protected BaseResponse mResponse;
    protected int mType;
    protected int mId;

    private static int requestMaxId = 0;

    protected static final int HTTP_REQUEST_TYPE = 1;

    public static final int CUSTOME_REQUEST_TYPE_BASE = 100;

    private List<WeakReference<RequestEventListener>> mListenerList = new LinkedList<WeakReference<RequestEventListener>>();

    public abstract void startRequest();

    protected BaseRequest(int requestType) {
        mType = requestType;
        mId = BaseRequest.requestMaxId;
        BaseRequest.requestMaxId ++;
    }
    
    public int getType() {
        return mType;
    }
    /**
     * Add listener to handle task event, including fail, success and message.
     *
     * @param listener, to handle task event
     */
    public void addListener(RequestEventListener listener) {
        if (listener == null) {
            return;
        }

        if (findListener(listener) == null) {
            mListenerList.add(new WeakReference<RequestEventListener>(listener));
        }
    }

    /**
     * Remove listener which has added into listener list.
     *
     * @param listener, which should be removed from the list
     */
    public void removeHttpListener(RequestEventListener listener) {
        if (listener == null) {
            return;
        }

        WeakReference<RequestEventListener> ref = findListener(listener);
        if (ref != null) {
            mListenerList.remove(ref);
        }
    }

    /**
     * Notify BaseTask that the response has arrived.
     */
    protected void notifyListener() {
        for (WeakReference<RequestEventListener> ref : mListenerList) {
            if (ref != null) {
                RequestEventListener listener = ref.get();
                if (listener != null) {
                    listener.handleResponse(this, this.mResponse);
                }
            }
        }
    }

    private WeakReference<RequestEventListener> findListener(RequestEventListener listener) {
        if (mListenerList == null) {
            return null;
        }

        for (WeakReference<RequestEventListener> ref : mListenerList) {
            if (ref.get() == listener) {
                return ref;
            }
        }
        return null;
    }
}
